﻿using System;
using System.Diagnostics;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace CustomControls
{
    public class OpenEditButton : Button
    {
        private Image _im1;
        private Button _b1;
        private string _programName;
        private string _targetName;
        static OpenEditButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(OpenEditButton), new FrameworkPropertyMetadata(typeof(OpenEditButton)));
        }
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            _im1 = Template.FindName("PART_im1", this) as Image;
            _b1 = Template.FindName("PART_b1", this) as Button;
            var assembly = Assembly.GetExecutingAssembly();
            var imageStream = assembly.GetManifestResourceStream("CustomControls.MC900441427.PNG");
            if (imageStream != null)
            {
                BitmapFrame bmp = BitmapFrame.Create(imageStream);
                if (_im1 != null) _im1.Source = bmp;
            }
            if (_b1 != null)
            {
                _b1.Click+= (s, a) =>
                {
                    if (Command != null && Command.CanExecute(_programName + "|" + _targetName))
                    {
                        if (String.IsNullOrEmpty(_programName) || String.IsNullOrEmpty(_targetName))
                        {
                            MessageBox.Show("Invalid program name or target name");
                            return;
                        }
                        Process.Start(_programName, _targetName);
                    }
                };
            }
        }
        #region ProgramName (DependencyProperty)
        public string ProgramName
        {
            get { return (string)GetValue(ProgramNameProperty); }
            set { SetValue(ProgramNameProperty, value); }
        }
        public static readonly DependencyProperty ProgramNameProperty =
            DependencyProperty.Register("ProgramName", typeof(string), typeof(OpenEditButton),
              new PropertyMetadata{PropertyChangedCallback = ProgramNameChanged});
        private static void ProgramNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            OpenEditButton oeb = d as OpenEditButton;
            if (oeb != null)
            {
                oeb._programName = e.NewValue.ToString();
            }
        }
        #endregion
        #region TargetName (DependencyProperty)
        public string TargetName
        {
            get { return (string)GetValue(TargetNameProperty); }
            set { SetValue(TargetNameProperty, value); }
        }
        public static readonly DependencyProperty TargetNameProperty =
            DependencyProperty.Register("TargetName", typeof(string), typeof(OpenEditButton),
              new PropertyMetadata{PropertyChangedCallback = TargetNameChanged});
        private static void TargetNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            OpenEditButton oeb = d as OpenEditButton;
            if (oeb != null)
            {
                oeb._targetName = e.NewValue.ToString();
            }
        }
        #endregion
    }
}
